import json
import time

from map.net_tools.urlTools import urlTools
import map.map_api.tencent_map_api as tencent_map_api
import map.db_tools as mysql_pool
import scenery_sql


def get_scenery(params):
    url = 'https://zwfw.mct.gov.cn/scenicspot'
    scenery_list = []
    start_time = time.time()
    pageSoup = urlTools.getPageSoup(url, params)
    end_time = time.time()
    print('==>>请求和解析耗时：{}秒'.format(end_time - start_time))
    sceneryBoxs = pageSoup.find_all(class_="wrap_1-list")[0].find_all("li")
    if sceneryBoxs == None:
        print("===>>>未获取到目标节点")
        return scenery_list

    for sceneryBox in sceneryBoxs:
        if sceneryBox.find("span") == None:
            if '无数据' in sceneryBox.text:
                print('===>>>页面无数据')
                continue
        district = sceneryBox.find("span").text
        name = sceneryBox.find("a").text
        print(f'==>>解析结果 district:{district} ,name:{name}')
        scenery = {'name': name, 'district': district}
        params = {'region': district, 'keyword': name, }
        response_str = tencent_map_api.suggestion(params)
        time.sleep(0.2)
        response_json = json.loads(response_str)
        if not response_json.get('data'):
            scenery_list.append(scenery)
            print(f'===>>>坐标查询错误：scenery:{scenery},response_json:{response_json}')
            continue
        data = response_json['data']
        if len(data) < 1:
            print(f'===>>>坐标查询错误：scenery:{scenery},response_json:{response_json}')
            scenery_list.append(scenery)
            continue
        address = data[0]['address']
        lat = data[0]['location']['lat']
        lng = data[0]['location']['lng']
        category = data[0]['category']
        print(f'==>>scenery:{scenery}')
        scenery['address'] = address
        scenery['lat'] = lat
        scenery['lng'] = lng
        scenery['category'] = category
        scenery_list.append(scenery)
    return scenery_list


def save_scenery_db(scenery_list: list):
    print('==>>开始入库')
    for scenery in scenery_list:
        count = mysql_pool.fetch_one(scenery_sql.count_scenery_info,
                                     scenery_sql.count_scenery_info_param(scenery['name'], scenery['district']))
        if count['count'] == 0:
            mysql_pool.insert(scenery_sql.insert_scenery_info,
                              scenery_sql.insert_scenery_info_param(scenery.get('name'), scenery.get('district'),
                                                                    scenery.get('address', '-'),
                                                                    scenery.get('lat', '-'), scenery.get('lng', '-')))
        else:
            print(f'===>>>不保存数据：{count}')
    print('==>>入库结束')

def do_reptile():

    pageNum = 1
    while pageNum <= 22:
        print('\n')
        print(f'==>>pageNum:{pageNum}')
        params = {'pageNum': pageNum}
        scenery_list = get_scenery(params)
        if len(scenery_list) > 0:
            print(scenery_list)
            save_scenery_db(scenery_list)
        pageNum = pageNum + 1

do_reptile()
